#!/usr/bin/env python
# -*-coding:UTF-8 -*-
'''
@Project ：爬虫-波波老师
@File：22-用户中心的用户信息.py
@Author ：文非
@Date：2021/3/1219:54
@Require： 爬取用户中心页的用户信息 在模拟登录后 点击头像超链接 发送请求 获取数据
'''

import requests
from lxml import etree
from chaojiying import Chaojiying_Client

def define(username, pwd, itemid):
    """验证码识别"""
    chaojiying = Chaojiying_Client(username, pwd, itemid)  # 用户中心>>软件ID 生成一个替换 96001
    # chaojiying = Chaojiying_Client('zwwlx1013', 'zwwlx1013', '913953')  # 用户中心>>软件ID 生成一个替换 96001
    im = open('./rencode.jpg', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    define_res = chaojiying.PostPic(im, 1902)['pic_str']
    print(define_res)
    return define_res

def main():
    # UA伪装
    headers = {
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
    }
    # 验证码获取
        # 1.指定url
    url = "http://www.renren.com"
        # 2.对验证码所在的首页发送请求
    response = requests.get(url=url, headers=headers)
        # 3.获取相应的页面数据
    define_text = response.text
        # 4.实例化一个etree对象
    tree = etree.HTML(define_text)
        # 5.解析验证码的img的src地址
    define_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
        # 6.拼接验证码url
        # 7.对拼接的url发送请求
    define_response = requests.get(url=define_url, headers=headers)
        # 8.获取返回的二进制图片数据
    define_data = define_response.content
        # 9.持久化保存到本地 将获取的结果当做模拟登录按钮的post请求的参数
    with open("./rencode.jpg","wb")as fp:
        fp.write(define_data)
    # 使用打码平台 对验证码识别
    username = 'zwwlx1013'
    pwd = 'zwwlx1013'
    itemid = '913953'
    result = define(username,pwd,itemid)
    print(result)
    login_url = "http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2021251841290"
    data = {
        "email": "13476923553",
        "icode": "",
        "origURL": "http://www.renren.com/home",
        "domain": "renren.com",
        "key_id": "1",
        "captcha_type": "web_login",
        "password": "5b44f3869e203e2510d8e45f1ba095f52fe82c615027c657b65f322286dc0e84",
        "rkey": "86854a257ebff1d95f0242cc03b67fa5",
        "f": "http%3A%2F%2Fwww.renren.com%2F975503334"
    }
    login_after_page = requests.post(url=login_url, data=data, headers=headers).text
    # print(login_after_page)
    # print(login_after_page.split("\"")[5])
    login_after_page = login_after_page.split("\"")[5]
    home_url = login_after_page
    home_page_response = requests.get(url=home_url,headers=headers)
    home_page = home_page_response.text
    # print(home_page)
    response_code = home_page_response.status_code
    # print(home_page)
    # print(response_code)
    with open("./renren.html", "w", encoding="utf-8")as fp:
        fp.write(home_page)
    # 爬取用户页面的个人主页的url

    # 实例化另一个etree
    tree_center = etree.HTML(home_page)
    center_url_wrong= tree_center.xpath('//div[@class="hd-wraper"]//div/dl/dt/a/@href')
    # print(center_url_wrong)
    center_url = "http://www.renren.com/975503334/profile"
    # print(center_url)
    #  1 手动复制cookies
    headers={
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
        "cookies":"anonymid=km62w655b9t6xo; depovince=GW; _r01_=1; JSESSIONID=abc6DWJH0Fdd66buPSLGx; ick_login=879a13db-f93a-466d-b731-c692e4cf60a3; taihe_bi_sdk_uid=9551df0bc4f8f45d3fec0f2aecb37701; taihe_bi_sdk_session=4a85a22f85a41d3b024d3e55fa12a503; ick=4a2781c5-c64a-43e1-993a-3addceafe106; __utmc=151146938; first_login_flag=1; ln_hurl=http://hdn.xnimg.cn/photos/hdn221/20201208/1055/h_main_HXFj_0c180003cc2d195a.jpg; __utma=151146938.945366404.1615540358.1615540358.1615542505.2; __utmz=151146938.1615542505.2.2.utmcsr=safe.renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/standalone/findpwd/resetpwd; wp_fold=0; jebecookies=5ec36a31-4c02-49f3-8130-a45b4fa28823|||||; _de=AD00763FB15B71667043FE0B58F9183D; p=b48956c1bf6d4ddf831759b3a569b3064; ln_uact=13476923553; t=b3396bf33af369cb22c3516505e22d034; societyguester=b3396bf33af369cb22c3516505e22d034; id=975503334; ver=7.0; xnsid=d97ca920; loginfrom=null"
    }
    # print(requests.get(url=center_url,headers=headers).text)
    center_page = requests.get(url=center_url, headers=headers).text
    with open("./人人个人中心.html","w",encoding="utf-8")as fp:
        fp.write(center_page)
        #  请求后得到的页面发现是登录页面而不是我们想要的用户中心的页面
        #  原因： 发起第二次基于个人的主页页面请求的时候，服务器并不知道该次请求是基于登录状态下的请求


if __name__ == "__main__":

    main()
